// 启动数据库
const MongoClient = require('mongodb').MongoClient
const dbUrl = 'mongodb://localhost:27017'
const dbName = 'myproject'
const client = new MongoClient(dbUrl)

const insertDocuments = function(db, callback) {
    // Get the documents collection
    const collection = db.collection('nameList');
    // Insert some documents
    collection.insertMany([
      {name : 'allen'}, 
      {name : 'tony'}, 
      {name : 'mary'}
    ], function(err, result) {
      console.log("Inserted 3 documents into the collection");
      callback(result);
    });
  }


module.exports = function() {
    client.connect((err) => {
        console.log('Connected successfully to server')
        const db = client.db(dbName)
    
        insertDocuments(db, function() {
            console.log('数据库关闭')
            client.close()
        })
    })
}